<?php

	class ListingClaimsController extends AppController
	{
		var $name = "ListingClaims";
		
		var $components = array('Auth','Email');
		var $helpers = array('Index');
		
		function claim($listingId)
		{
			if(!cr("ListingConfig.business_owner"))
			{
				$this->redirect($this->referer());
			}
			$this->ListingClaim->Listing->recursive = -1;
			$listing = $this->ListingClaim->Listing->findById($listingId);
			if(!$listing || $listing['Listing']['status'] != Listing::PUBLISH)
			{
				$this->redirect("/");
			}
			if($listing['Listing']['owner_id'])
			{
				$this->errorMsg(__("This listing has been claimed already. Please contact administrator if you think this is an error."));
				$this->redirect("/");
			}
			$this->set("listing",$listing);
			$claimId = $this->ListingClaim->field('id',array("user_id"=>$this->Session->read("Auth.User.id"),'listing_id'=>$listingId));
			$listingClaim = null;
			if($claimId)
			{
				$listingClaim = $this->ListingClaim->findById($claimId);	
			}
			if($this->data)
			{
				$this->ListingClaim->create();
				$this->ListingClaim->id = $claimId;
				$this->data['ListingClaim']['listing_id'] = $listingId;
				$this->data['ListingClaim']['user_id'] = $this->Session->read("Auth.User.id");
				if(!$this->data['ListingClaim']['check'])
				{
					$this->data['ListingClaim']['check'] = "";
				}
			 	if($this->ListingClaim->save($this->data))
				{
					if(!$claimId)
					{
						$this->Email->reset();
						$this->prepareEmail();
						$this->Email->to = cr("cmsConfig.Backend.email");
						$this->Email->subject = "New claim on ".$listing['Listing']['title'];
						$this->Email->template = "new_claim_admin";
						$this->Email->sendAs = "both";
						$this->Email->send();	
					}
					$this->successMsg(__("Your claim has been sent to admin for verification. You will be notified by E-mail when your request is accepted."));
					$this->redirect($listing['Listing']['viewUrl']);
				}else
				{
					$this->errorMsg(__("Please fix the error below."));
				}
			}else
			{
				if($listingClaim)
				{
					$this->data = $listingClaim;
				}else
					{
						$this->data['ListingClaim']['email'] = $this->Session->read("Auth.User.email"); 
					}
			}
			$this->pageTitle = sprintf(__("Claim %s"),$listing['Listing']['title']);
		}
		
		function admin_index($listingId = null)
		{
			$this->ListingClaim->Listing->recursive = -1;
			App::import('Vendor','Searchable.GigaSearch');
			$GigaSearch = new GigaSearch($this->ListingClaim);
			$condition  = array();
			if($this->data)
			{
				$url = $GigaSearch->toUrlArray($this->data['ListingClaim']);
				$url = am(array('action'=>"index"),$url);
				$this->redirect($url);
			}
			$query = $GigaSearch->toDataFromUrl($this->ListingClaim->getFields("group","search"),$this->params['named']);
			if($query)
			{
				$this->data['ListingClaim'] = $query;
				$fields = $this->ListingClaim->getFields("group","search");
				$fieldCopy = array_combine($fields,$fields);
				unset($fieldCopy['listing_id']);
				$condition = $GigaSearch->createConditions($fieldCopy,$query);
				if(isset($query['listing_id']))	
				{
					$ListingSearch = new GigaSearch($this->ListingClaim->Listing);
					$condition2 = $ListingSearch->createConditions(array("title"),array("title"=>$query['listing_id']));
					$condition = am($condition,$condition2);
				}
			}
			$this->paginate['order'] = "ListingClaim.created DESC";
			$this->paginate['conditions'] = array();
			$paginateLimit = 10;
			if(isset($this->ListingClaim->paginateLimit))
			{
				$paginateLimit = $this->ListingClaim->paginateLimit;
			}
			$this->paginate['limit'] = $paginateLimit;
			if($listingId)
			{
				$listing = $this->ListingClaim->Listing->findById($listingId);
				$this->paginate['conditions'] = array("ListingClaim.listing_id"=>$listingId);
				$this->pageTitle = sprintf(__("Claims on %s"),$listing['Listing']['title']);
				$this->set("listing",$listing);
			}
				if($condition)
				{
					$this->paginate['conditions'] = am($this->paginate['conditions'],$condition);
				}	
			$this->ListingClaim->bindModel(array("belongsTo"=>array("User"=>array("className"=>"Users.User"))),false);
			$listingClaims = $this->paginate();
			$this->set('listingClaims',$listingClaims);
			$this->helpers[] = "Searchable.SearchForm";
		}
		
		function admin_accept($listingClaimId)
		{
			$listingClaim = $this->ListingClaim->findById($listingClaimId);
			$ListingModel = cri("Listing");
			if($listingClaim['ListingClaim']['listing_id'])
			{
				$ListingModel->id = $listingClaim['ListingClaim']['listing_id'];
				$UserModel = cri("Users.User");
				$UserModel->recursive = -1;
				$ListingModel = cri("Listing");
				$ListingModel->recursive = -1;
				$this->ListingClaim->id = $listingClaim['ListingClaim']['id'];
				$this->ListingClaim->saveField("status",ListingClaim::STATUS_ASSIGNED);
				$user = $UserModel->findById($listingClaim['ListingClaim']['user_id']);
				$listing = $ListingModel->findById($listingClaim['ListingClaim']['listing_id']);
				$ListingModel->id = $listingClaim['ListingClaim']['listing_id'];
				$ListingModel->saveField('owner_candidate_id',NULL);
				$ListingModel->saveField('owner_id',$listingClaim['ListingClaim']['user_id']);
				App::import("Vendor","EmailOperation");
				EmailOperation::MailOnClaimAccept($this, $user, $listing);
				//set other user as reject.
				$otherUsers = $this->ListingClaim->find("list",array("conditions"=>array('listing_id'=>$listingClaim['ListingClaim']['listing_id'],'id <>'=>$listingClaim['ListingClaim']['id']),'fields'=>array("id",'user_id')));
				foreach($otherUsers as $claimId => $otherUser)
				{
					$this->setAsRejected($claimId, $listing, $otherUser);
				}
				$this->successMsg(__("Listing has been assigned."));
				$this->redirect($this->referer(array("action"=>"index")));
			}
			$this->redirect('/');
		}
		
		function admin_reject($listingClaimId)
		{
			$listingClaim = $this->ListingClaim->findById($listingClaimId);
			$ListingModel = cri("Listing");
			if($listingClaim['ListingClaim']['listing_id'])
			{
				$ListingModel = cri("Listing");
				$ListingModel->recursive = -1;
				$listing = $ListingModel->findById($listingClaim['ListingClaim']['listing_id']);
				$this->setAsRejected($listingClaimId, $listing, $listingClaim['ListingClaim']['user_id']);
				if($listing['Listing']['owner_id'] && $listing['Listing']['owner_id'] == $listingClaim['ListingClaim']['user_id'])
				{
					 $ListingModel->id = $listingClaim['ListingClaim']['listing_id'];
					 $ListingModel->saveField("owner_id",null);
				}
				$this->redirect($this->referer(array("action"=>"index")));
			}
			$this->redirect('/');
		}
		
		protected function setAsRejected($listinClaimId,$listing,$userId)
		{
			$UserModel = cri("Users.User");
			$UserModel->recursive = -1;
			$user = $UserModel->findById($userId);
			$this->ListingClaim->id = $listinClaimId;
			$this->ListingClaim->saveField("status",ListingClaim::STATUS_REJECTED);
			App::import("Vendor","EmailOperation");
			EmailOperation::MailOnClaimReject($this, $user, $listing);
		}
	}
?>